#pragma once
#include<iostream>
#include<algorithm>
#include<assert.h>
#include<list>
using namespace std;
namespace MyList
{
	template<class T>
	struct list_node
	{
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;
		list_node(const T& data = T())
			:_data(data),
			_next(nullptr),
			_prev(nullptr)
		{
		}
	};

	template<class T, class Ref, class Ptr>
	struct list_iterator
	{
		typedef list_node<T> Node;
		typedef list_iterator<T, Ref, Ptr> Self;
		list_iterator(Node* n)
		{
			node = n;
		}
		list_iterator(const Self& it)
		{
			node = it.node;
		}
		Self& operator++()
		{
			node = node->_next;
			return *this;
		}
		Self& operator--()
		{
			node = node->_prev;
			return *this;
		}
		Self operator++(int)
		{
			Self tmp(*this);
			node = node->_next;
			return tmp;
		}
		Self operator--(int)
		{
			Self tmp(*this);
			node = node->_prev;
			return tmp;
		}
		bool operator!=(const Self& it)
		{
			return node != it.node;
		}
		bool operator==(const Self& it)
		{
			return node == it.node;
		}
		Ref operator*() const
		{
			return node->_data;
		}
		Ptr operator->() const
		{
			return &(node->_data);
		}
		Node* node;
	};

	template<class T>
	class list
	{
	public:
		typedef list_node<T> Node;
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;
		iterator begin()
		{
			return iterator(phead->_next);
		}
		iterator end()
		{
			return iterator(phead);
		}
		const_iterator begin() const
		{
			return const_iterator(phead->_next);
		}
		const_iterator end() const
		{
			return const_iterator(phead);
		}
		bool empty()
		{
			return phead->_next == phead;
		}
		void empty_list()
		{
			phead = new Node();
			phead->_next = phead;
			phead->_prev = phead;
		}
		list()
		{
			empty_list();
		}
		list(const list<T>& lt)
		{
			empty_list();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}
		list(size_t n, const T& val = T())
		{
			empty_list();
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}
		void swap(list<T>& lt)
		{
			std::swap(phead, lt.phead);
			std::swap(size, lt.size);
		}
		~list()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
				it++;
			}
			delete phead;
			phead = nullptr;
			size = 0;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
				it++;
			}
			phead->_next = phead;
			phead->_prev = phead;
			size = 0;
		}
		void push_back(const T& val)
		{
			/*Node* newnode = new Node(x);
			Node* tail = phead->_prev;
			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = phead;
			phead->_prev = newnode;*/

			insert(end(), val);
		}
		iterator insert(iterator position, const T& val)
		{
			Node* cur = position.node;
			Node* prev = cur->_prev;
			Node* newnode = new Node(val);
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			++size;
			return iterator(newnode);
		}
		void insert(iterator position, size_t n, const T& val)
		{
			Node* cur = position.node;
			Node* prev = cur->_prev;
			for (size_t i = 0; i < n; i++)
			{
				Node* newnode = new Node(val);
				prev->_next = newnode;
				newnode->_prev = prev;
				prev = newnode;
			}
			prev->_next = cur;
			cur->_prev = prev;
			size += n;
		}
		iterator erase(iterator position)
		{
			assert(!empty());
			assert(position != end());
			Node* del = position.node;
			Node* prev = del->_prev;
			Node* cur = del->_next;

			prev->_next = cur;
			cur->_prev = prev;
			delete del;
			del = nullptr;
			--size;
			return iterator(cur);
		}
		void push_front(const T& val)
		{
			insert(begin(), val);
		}
		void pop_front()
		{
			assert(!empty());
			erase(begin());
		}
		void pop_back()
		{
			assert(!empty());
			erase(--end());
		}
		T& front()
		{
			return *begin();
		}
		const T& front() const
		{
			return *begin();
		}
		T& back()
		{
			return *(--end());
		}
		const T& back() const
		{
			return *(--end());
		}
	private:
		Node* phead;
		size_t size;
	};
	template<class T>
	void swap(T& left, T& right)
	{
		T tmp(left);
		left = right;
		right = tmp;
	}
	template<class T>
	void swap(list<T>& left, list<T>& right)
	{
		left.swap(right);
	}
}